import {
    request
} from "../../../request/index.js";
var util = require('../../../utils/util.js');
Page({
    data: {
        address: {
            id: null,
            consigneeName: '',
            consigneeMobile: '',
            universityName: '',
            campusName: '',
            buildingName: '',
            detailInfo: '',
            userId: 0,
            isDefault: 0
        },
        addressId: 0
    },
    onLoad: function (options) {
        const {
            id
        } = options;
        this.getAddressDetail(id);
    },
    bindinputName(event) {
        let address = this.data.address;
        address.consigneeName = event.detail.value;
        this.setData({
            address: address
        });
    },

    bindinputMobile(event) {
        let address = this.data.address;
        address.consigneeMobile = event.detail.value;
        this.setData({
            address: address
        });
    },
    bindinputUniversity(event) {
        let address = this.data.address;
        address.universityName = event.detail.value;
        this.setData({
            address: address
        });
    },
    bindinputCampus(event) {
        let address = this.data.address;
        address.campusName = event.detail.value;
        this.setData({
            address: address
        });
    },
    bindinputBuilding(event) {
        let address = this.data.address;
        address.buildingName = event.detail.value;
        this.setData({
            address: address
        });
    },
    bindinputDetail(event) {
        let address = this.data.address;
        address.detailInfo = event.detail.value;
        this.setData({
            address: address
        });
    },
    async getAddressDetail(id) {
        if (id) {
            const res = await request({
                url: "/consignees/getOne",
                data: {
                    id
                }
            });
            if (res.data.data) {
                this.setData({
                    address: res.data.data
                });
            }
        }
    },
    bindIsDefault() {
        let address = this.data.address;
        address.isDefault = !address.isDefault;
        this.setData({
            address: address
        });
    },
    async saveAddress() {
        const consignee = this.data.address;
        if (consignee.consigneeName == '') {
            util.showErrorToast('请输入姓名');
            return false;
        }
        if (consignee.consigneeMobile == '') {
            util.showErrorToast('请输入手机号码');
            return false;
        }
        if (consignee.universityName == 0) {
            util.showErrorToast('请输入校名');
            return false;
        }
        if (consignee.campusName == 0) {
            util.showErrorToast('请输入校区');
            return false;
        }
        if (consignee.buildingName == 0) {
            util.showErrorToast('请输入楼名');
            return false;
        }
        if (consignee.detailInfo == '') {
            util.showErrorToast('请输入详细位置');
            return false;
        }
        const userId = redis.get('userId');;
        consignee.userId = userId;
        const res = await request({
            url: "/consignees/saveConsignee",
            method: "post",
            data: consignee
        });

        if (res.statusCode == 200) {
            util.showSuccessToast(res.data.data);
            setTimeout(function () {
                wx.navigateBack({
                    url: "/pages/ucenter/address/index"
                })
            }, 1000)
        } else {
            util.showErrorToast(res.data.data);
        }
        console.log(res);
    },
})